home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 634 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.3 KB

  1. Path: tbj.dec.com!diamond
  2. From: diamond@tbj.dec.com (Norman Diamond)
  3. Newsgroups: comp.std.c
  4. Subject: Re: Restrictions on qsort compare function?
  5. Date: 23 Mar 1996 00:59:54 GMT
  6. Organization: Digital Equipment Corporation Japan , Tokyo
  7. Message-ID: <4iviea$bd6@usenet.pa.dec.com>
  8. References: <4iokop$h4p@lyra.csx.cam.ac.uk> <4iqjar$2m9@usenet.pa.dec.com> <1996Mar21.113301.2622@sq.com> <4it51b$ng8@usenet.pa.dec.com> <4iukhc$5nr@rdsunx.crd.ge.com>
  9. Reply-To: diamond@tbj.dec.com (Norman Diamond)
  10. NNTP-Posting-Host: jit533.tbj.dec.com
  11.  
  12. In article <4iukhc$5nr@rdsunx.crd.ge.com>, volpe@bart.crd.ge.com (Christopher R. Volpe) writes:
  13. >In article <4it51b$ng8@usenet.pa.dec.com>, diamond@tbj.dec.com (Norman Diamond) writes:
  14. >>>>Just one sentence earlier, you gave the exact reason why
  15. >>>>it doesn't matter if you do "a > b" instead of "a - b".
  16.  
  17. >>>Of course it matters.  With this function, if compar(x,y) > 0, then
  18. >>>compar(y,x) == 0.  This is not antisymmetric.
  19.  
  20. >>Argh!  I was remembering what the hardware usually does for comparison
  21. >>instructions, and forgot that C operators lose part of that information.
  22. >>I must be getting Cnile.
  23.  
  24. >>>(a > b)? 1: (a < b)? -1: 0
  25.  
  26. >>Yup.  Incidentally, do you think the average implementation's
  27. >>interprocedural optimization phase will change this user function into
  28. >>one machine instruction in the library's qsort() function :-?
  29.  
  30. >I'd be quite impressed if it did.
  31. >Alternatively, how about this:
  32. >    return ((signed int) ((unsigned int)a - (unsigned int)b));
  33.  
  34. If this alternative is a reply to my snarky question about quality of
  35. implementation (which was a bit off-topic) then the answer is that it
  36. will probably work even under "reasonable" implementations.  However,
  37. if you're asking what the standard says about your alternative, the
  38. effect is not strictly conforming and again might cause undefined
  39. behavior if the standard requires the comparison function to be reasonable.
  40. The problem is that the conversion from unsigned int to signed int will
  41. be implementation-defined about half the time.
  42. --
  43.  <<  If this were the company's opinion, I would not be allowed to post it.  >>
  44. "I paid money for this car, I pay taxes for vehicle registration and a driver's
  45. license, so I can drive in any lane I want, and no innocent victim gets to call
  46. the cops just 'cause the lane's not goin' the same direction as me" - J Spammer
  47.